package com.pk.platform.business.report.service.impl;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import com.pk.platform.business.report.dao.IOperateLogReportDao;
import com.pk.platform.business.report.service.IOperateLogReportService;
import com.pk.platform.business.report.vo.OperateLogVO;
import com.pk.platform.util.DateConverter;

public class OperateLogReportServiceImpl implements IOperateLogReportService {

	private IOperateLogReportDao operateLogReportDao;
	
	public InputStream queryOperateLogReport(OperateLogVO olvo) {
		// 将OutputStream转化为InputStream
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		putDataOnOutputStream(out, olvo);
		return new ByteArrayInputStream(out.toByteArray());
	}
	
	private void putDataOnOutputStream(OutputStream os, OperateLogVO olvo) {
		WritableWorkbook workbook = null;
		try {
			workbook = Workbook.createWorkbook(os);
			List<Map<String, Object>> reportList = operateLogReportDao.queryOperateLogReport(olvo);
			WritableFont wf1 = new WritableFont(WritableFont.TIMES, 13,	WritableFont.BOLD);
			WritableCellFormat wcf1 = new WritableCellFormat(wf1);
			wcf1.setAlignment(Alignment.CENTRE);
			wcf1.setVerticalAlignment(VerticalAlignment.CENTRE);
			wcf1.setWrap(true);
			WritableFont wf2 = new WritableFont(WritableFont.TIMES, 11,	WritableFont.BOLD);
			WritableCellFormat wcf2 = new WritableCellFormat(wf2);
			wcf2.setAlignment(Alignment.CENTRE);
			wcf2.setVerticalAlignment(VerticalAlignment.CENTRE);
			
			WritableSheet sheet = workbook.createSheet("操作日志报表", 0);
			Label lb = null;
			Map<String, Object> map;
			sheet.mergeCells(0, 0, 4, 1);
			sheet.setColumnView(0, 35);
			sheet.setColumnView(1, 20);
			sheet.setColumnView(2, 20);
			sheet.setColumnView(3, 15);
			sheet.setColumnView(4, 15);
			String head = "操作日志报表";
			int n = 0;
			lb = new jxl.write.Label(0, 0, head,wcf1);
			sheet.addCell(lb);
			lb = new jxl.write.Label(n++, 2, "描述",wcf2);
			sheet.addCell(lb);
			lb = new jxl.write.Label(n++, 2, "操作时间",wcf2);
			sheet.addCell(lb);
			lb = new jxl.write.Label(n++, 2, "操作结果",wcf2);
			sheet.addCell(lb);
			lb = new jxl.write.Label(n++, 2, "用户名",wcf2);
			sheet.addCell(lb);
			lb = new jxl.write.Label(n++, 2, "所属幼儿园",wcf2);
			sheet.addCell(lb);
			
			for (int i = 0; i < reportList.size(); i++) {
				int x = i + 3;
				int y = 0;
				map = reportList.get(i);
				lb = new Label(y++, x, map.get("description")==null?"":map.get("description").toString());
				sheet.addCell(lb);
				lb = new Label(y++, x, map.get("operateTime")==null?"":DateConverter.dateToString((Date)map.get("operateTime"), DateConverter.FORMAT_THREE));
				sheet.addCell(lb);
				lb = new Label(y++, x, map.get("operateResult")==null?"":map.get("operateResult").toString());
				sheet.addCell(lb);
				lb = new Label(y++, x, map.get("username")==null?"":map.get("username").toString());
				sheet.addCell(lb);
				lb = new Label(y++, x, map.get("kgName")==null?"":map.get("kgName").toString());
				sheet.addCell(lb);
			}
			workbook.write();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally
		{
			try
			{
				workbook.close();
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public IOperateLogReportDao getOperateLogReportDao() {
		return operateLogReportDao;
	}

	public void setOperateLogReportDao(IOperateLogReportDao operateLogReportDao) {
		this.operateLogReportDao = operateLogReportDao;
	}

}
